package com.hdax.dmw.item.mapstruct;

import com.hdax.dmw.base.DmImage;
import com.hdax.dmw.dtos.item.HotItemDto;
import com.hdax.dmw.item.DmItem;
import com.hdax.dmw.openfeign.item.BaseFeign;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.Mappings;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@Mapper(componentModel = "spring")
public abstract class HotItemDtoMapper {

    @Mappings({
            @Mapping(
                    target = "imgUrl",
                    expression = "java(getImg(source.getId()))"
            )
    })
    public abstract HotItemDto toTarget(DmItem source);
    public abstract List<HotItemDto> toTargetList(List<DmItem> sources);

    //声明远程调用接口
    @Autowired
    private BaseFeign baseFeign;

    public String getImg(Long itemId){
        DmImage image = baseFeign.image(itemId, 2, 1);
        return image == null?
        "https://z3.ax1x.com/2021/08/10/fGjQcd.jpg":image.getImgUrl();
    }
}
